package com.iot.manager.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.iot.manager.R;
import com.iot.manager.data.models.Device;
import com.iot.manager.network.RetrofitClient;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EditDeviceDialog extends Dialog {
    private final Device device;
    private OnDeviceUpdatedListener listener;
    private TextInputEditText etName;
    private TextInputEditText etType;
    private MaterialButton btnSave;
    private MaterialButton btnCancel;

    public interface OnDeviceUpdatedListener {
        void onDeviceUpdated(Device device);
    }

    public EditDeviceDialog(@NonNull Context context, Device device) {
        super(context);
        this.device = device;
    }

    public void setOnDeviceUpdatedListener(OnDeviceUpdatedListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_edit_device, null);
        setContentView(view);

        etName = view.findViewById(R.id.et_name);
        etType = view.findViewById(R.id.et_type);
        btnSave = view.findViewById(R.id.btnSave);
        btnCancel = view.findViewById(R.id.btnCancel);

        etName.setText(device.getName());
        etType.setText(device.getType());

        btnSave.setOnClickListener(v -> {
            String name = etName.getText().toString().trim();
            String type = etType.getText().toString().trim();

            if (name.isEmpty()) {
                etName.setError("设备名称不能为空");
                return;
            }

            updateDevice(name, type);
            dismiss();
        });

        btnCancel.setOnClickListener(v -> dismiss());
    }

    private void updateDevice(String name, String type) {
        Device updatedDevice = new Device();
        updatedDevice.setName(name);
        updatedDevice.setType(type);

        RetrofitClient.getInstance().getApiService().updateDevice(device.getDeviceId(), updatedDevice)
            .enqueue(new Callback<Device>() {
                @Override
                public void onResponse(Call<Device> call, Response<Device> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        if (listener != null) {
                            listener.onDeviceUpdated(response.body());
                        }
                        Toast.makeText(getContext(), "设备信息更新成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(), "设备信息更新失败", Toast.LENGTH_SHORT).show();
                    }
                }

                @Override
                public void onFailure(Call<Device> call, Throwable t) {
                    Toast.makeText(getContext(), "网络错误: " + t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
    }
} 